﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace DAL
{
    public class Order
    {

        public static int insert(OrderEntity order)
        {
            return Convert.ToInt32(DbHelper.ExecuteScalar("p_Order_insert",
              order.Source,
              order.OrderNo,
              order.CustomerId,
              order.Amount,
              order.StandardAmount,
              order.TypeCount,
              order.FireightFee,
              order.Taxfee,
              order.Status,
              order.Note,             
              order.ReceiveDt,
              order.CompanyId
               ));
        }
        public static int insertWithDynamicOrder(OrderEntity order)
        {
            return Convert.ToInt32(DbHelper.ExecuteScalar("p_Order_insertWithDynamicOrder",
              order.Source,
              order.OrderNo,
              order.CustomerId,
              order.Amount,
              order.StandardAmount,
              order.TypeCount,
              order.FireightFee,
              order.Taxfee,
              order.Status,
              order.Note,
              order.OrderDt,
              order.ReceiveDt,
              order.CompanyId
               ));
        }

        public static int insertDynamicOrder(OrderEntity order)
        {
            return Convert.ToInt32(DbHelper.ExecuteScalar("p_DynamicOrder_insert",
              order.Source,
              order.CustomerId,
              order.Amount,
              order.StandardAmount,
              order.TypeCount,
              order.FireightFee,
              order.Taxfee,
              order.Status,
              order.Note,
              order.ReceiveDt,
              order.CompanyId));
        }

        public static void update(OrderEntity _order)
        {
            DbHelper.ExecuteNonQuery("p_Order_update",_order.Orderid,_order.Amount,_order.FireightFee,_order.Taxfee,
                _order.Status,_order.ServiceNote,_order.ReceiveDt,_order.CloseReason,_order.AdminId,_order.CompanyId);
        }

        public static OrderEntity getByDynamicOrderId(int orderid)
        {
            DataRow dr = DbHelper.ExecuteDataRow("p_DynamicOrder_getById", orderid);
            OrderEntity _order = new OrderEntity();
            if (dr != null)
            {
                _order.Orderid = orderid;
                _order.Amount = Convert.ToDecimal(dr["amount"]);
                _order.Status = dr["status"].ToString();
                _order.StatusName = dr["orderStatus"].ToString();
                _order.Note = dr["note"].ToString();
                _order.ReceiveDt = Convert.ToDateTime(dr["receiveDt"].ToString());
                _order.CompanyId = Convert.ToInt32(dr["companyid"].ToString());
            }
            return _order;
        }

        public static OrderEntity getById(int orderid)
        {
            DataRow dr = DbHelper.ExecuteDataRow("p_Order_getById", orderid);
            OrderEntity _order = new OrderEntity();
            if (dr != null)
            {
                _order.Orderid = orderid;
                _order.Source = dr["Source"].ToString();
                _order.OrderNo = dr["OrderNo"].ToString();
                _order.CustomerId =Convert.ToInt32( dr["CustomerId"]);
                _order.Amount = Convert.ToDecimal(dr["Amount"]);
                _order.StandardAmount = Convert.ToDecimal(dr["StandardAmount"]);
                _order.TypeCount = Convert.ToInt32(dr["typecount"]);
                _order.FireightFee = Convert.ToDecimal(dr["FireightFee"]);
                _order.Taxfee = Convert.ToDecimal(dr["Taxfee"]);
                _order.Status = dr["Status"].ToString();
                _order.Note = dr["Note"].ToString();
                _order.ServiceNote = dr["ServiceNote"].ToString();
                _order.OrderDt=Convert.ToDateTime(dr["OrderDt"]);
                _order.ReceiveDt = Convert.ToDateTime(dr["ReceiveDt"]);
                if (dr["UpdateStatusdt"] != DBNull.Value)
                    _order.UpdateStatusdt=Convert.ToDateTime(dr["UpdateStatusdt"]);
                _order.CloseReason =dr["CloseReason"].ToString();
                _order.RbNo=dr["RbNo"].ToString();
                if (dr["AdminId"] != DBNull.Value)
                    _order.AdminId=Convert.ToInt32(dr["AdminId"]);
               _order.CompanyId=Convert.ToInt32(dr["CompanyId"]);
            }
            return _order;
        }

        public static OrderEntity getDynamicById(int orderid)
        {
            DataRow dr = DbHelper.ExecuteDataRow("p_DynamicOrder_selectByOrderId", orderid);
            OrderEntity order = new OrderEntity();
            if (dr != null)
            {
                order.Source = dr["Source"].ToString();
                order.CustomerId = Convert.ToInt32(dr["CustomerId"]);
                order.TypeCount = Convert.ToInt32(dr["typecount"]);
                order.FireightFee = Convert.ToDecimal(dr["FireightFee"]);
                order.Taxfee = Convert.ToDecimal(dr["Taxfee"]);
                order.Note = dr["Note"].ToString();
                order.ServiceNote = dr["ServiceNote"].ToString();
                order.OrderDt = Convert.ToDateTime(dr["OrderDt"]);
                order.ReceiveDt = Convert.ToDateTime(dr["ReceiveDt"]);
                order.CloseReason = dr["CloseReason"].ToString();
                order.RbNo = dr["RbNo"].ToString();
                order.CompanyId = Convert.ToInt32(dr["CompanyId"]);
            }
            return order;
        }

        public static void updateStatus(OrderEntity _order)
        {
            DbHelper.ExecuteNonQuery("dbo.p_Order_updateStatus", _order.Orderid, _order.Status,_order.AdminId);
        }

        public static int dymanicOrderUnsale(string orderNo)
        {
            return Convert.ToInt32(DbHelper.ExecuteScalar("p_OrderItem_Unsale", orderNo));
        }
        public static void updateStatusDate(int orderId, DateTime updateStatusDt)
        {
            DbHelper.ExecuteNonQuery("p_Order_updateStatusDate", orderId, updateStatusDt);
        }

        /// <summary>
        /// 给物流部查看的订单列表并可以导出excel
        /// </summary>
        /// <param name="where"></param>
        /// <returns></returns>
        public static DataTable listByImportExcel(string where)
        {
            return DbHelper.ExecuteDataTable("p_Order_listByImportExcel", where);
        }

        /// <summary>
        /// 订单汇总
        /// </summary>
        /// <param name="companyId"></param>
        /// <returns></returns>
        public static DataTable listWithCollect(int companyId)
        {
            return DbHelper.ExecuteDataTable("p_Order_listWithCollect", companyId);
        }
        public static DataTable getTotal(string where)
        {
            return DbHelper.ExecuteDataTable("p_Order_getTotal", where);
        }

        public static void updateDynamicOrder(int orderId, string orderNo, int adminId)
        {
            DbHelper.ExecuteNonQuery("p_DynamicOrder_update", orderId, orderNo, adminId);
        }

        public static void updateDynamicOrderByService(OrderEntity order)
        {
            DbHelper.ExecuteNonQuery("p_DynamicOrder_updateByService", order.Orderid, order.ReceiveDt, order.ServiceNote);
        }

        public static void updateDynamiceOrderByOrderId(int orderid,int adminid)
        {
            DbHelper.ExecuteNonQuery("p_DynamicOrder_updateByOrderId", orderid,adminid);
        }

        public static void closeDynamicOrder(int orderid, string closeReason)
        {
            DbHelper.ExecuteNonQuery("p_DynamicOrder_closeOrder", orderid, closeReason);
        }

        public static DataRow listPreOrderInfo(int companyId, string orderNo)
        {
            return DbHelper.ExecuteDataRow("[dbo].[p_Order_listPreOrderInfo]", companyId, orderNo);
        }

        public static int countTodayDynamicOrder()
        {
            int totalCount = 0;
            object objectValue = DbHelper.ExecuteScalar("[dbo].[p_dynamic_order_countToday]");
            if (objectValue!=null)
            {
                int.TryParse(objectValue.ToString(),out totalCount);
            }
            return totalCount;
        }
    }
}
